package com.ruoyi.cookbook.controller;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cookbook.domain.Recipe;
import com.ruoyi.cookbook.service.IRecipeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 宠物管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/cookbook")
public class RecipeController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(RecipeController.class);
    
    @Autowired
    private IRecipeService recipeService;

    /**
     * 查询宠物列表
     */
    @Anonymous
    @GetMapping("/recipe/list")
    public TableDataInfo list(Recipe recipe)
    {
        log.info("访问宠物列表接口...");
        startPage();
        List<Recipe> list = recipeService.selectRecipeList(recipe);
        return getDataTable(list);
    }

    /**
     * 导出宠物列表
     */
    @Log(title = "宠物管理", businessType = BusinessType.EXPORT)
    @GetMapping("/recipe/export")
    public AjaxResult export(Recipe recipe)
    {
        List<Recipe> list = recipeService.selectRecipeList(recipe);
        ExcelUtil<Recipe> util = new ExcelUtil<Recipe>(Recipe.class);
        return util.exportExcel(list, "宠物数据");
    }

    /**
     * 获取宠物详细信息
     */
    @Anonymous
    @GetMapping(value = "/recipe/{recipeId}")
    public AjaxResult getInfo(@PathVariable("recipeId") Long recipeId)
    {
        return success(recipeService.selectRecipeById(recipeId));
    }

    /**
     * 新增宠物
     */
    @Log(title = "宠物管理", businessType = BusinessType.INSERT)
    @PostMapping("/recipe")
    public AjaxResult add(@RequestBody Recipe recipe)
    {
        return toAjax(recipeService.insertRecipe(recipe));
    }

    /**
     * 修改宠物
     */
    @Log(title = "宠物管理", businessType = BusinessType.UPDATE)
    @PutMapping("/recipe")
    public AjaxResult edit(@RequestBody Recipe recipe)
    {
        return toAjax(recipeService.updateRecipe(recipe));
    }

    /**
     * 删除宠物
     */
    @Log(title = "宠物管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/recipe/{recipeIds}")
    public AjaxResult remove(@PathVariable Long[] recipeIds)
    {
        return toAjax(recipeService.deleteRecipeByIds(recipeIds));
    }
    
    /**
     * 提交领养申请
     */
    @Anonymous
    @Log(title = "宠物领养", businessType = BusinessType.UPDATE)
    @PostMapping("/recipe/adopt")
    public AjaxResult adopt(@RequestBody Recipe recipe)
    {
        Long recipeId = recipe.getRecipeId();
        if (recipeId == null) {
            return error("宠物ID不能为空");
        }
        
        // 获取当前宠物信息
        Recipe existRecipe = recipeService.selectRecipeById(recipeId);
        if (existRecipe == null) {
            return error("宠物不存在");
        }
        
        // 检查宠物是否可领养
        if (!"1".equals(existRecipe.getStatus())) {
            return error("该宠物当前状态不可领养");
        }
        
        // 更新为已确认领养状态(2)
        existRecipe.setStatus("2");
        // 如果有其他领养相关信息，可以在这里设置
        
        return toAjax(recipeService.updateRecipe(existRecipe));
    }
    
    /**
     * 取消领养申请
     */
    @Anonymous
    @Log(title = "取消领养", businessType = BusinessType.UPDATE)
    @PutMapping("/adoption/cancel/{recipeId}")
    public AjaxResult cancelAdoption(@PathVariable("recipeId") Long recipeId)
    {
        if (recipeId == null) {
            return error("宠物ID不能为空");
        }
        
        // 获取当前宠物信息
        Recipe existRecipe = recipeService.selectRecipeById(recipeId);
        if (existRecipe == null) {
            return error("宠物不存在");
        }
        
        // 检查宠物是否处于已确认领养状态
        if (!"2".equals(existRecipe.getStatus())) {
            return error("该宠物当前状态不是已确认领养状态，无法取消");
        }
        
        // 更新为待领养状态(1)
        existRecipe.setStatus("1");
        
        return toAjax(recipeService.updateRecipe(existRecipe));
    }
} 